package com.edais.service.imp;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.edais.dao.RateRisesCouponsDao;
import com.edais.domain.ChannelWelfareParameters;
import com.edais.domain.RateRisesCoupons;
import com.edais.service.WelfareService;

/**
 * 加息券福利发放
 * @author zhouyj
 *
 */
@Service("interestRateCouponWelfareServiceImpl")

public class InterestRateCouponWelfareServiceImpl implements WelfareService {

	private static final Log logger = LogFactory.getLog(InterestRateCouponWelfareServiceImpl.class);
	
	@Autowired
	private RateRisesCouponsDao rateRisesCouponsDao;
	

	@Override
	public Map<String,Object> addWelfare(ChannelWelfareParameters channelWelfareParameters,Map<String,Object> paramsMap) throws Exception {
		logger.info("******发放加息券福利，福利参数id："+channelWelfareParameters.getId()+",发放用户"+paramsMap.get("userId")+"********");
		String mobile = paramsMap.get("mobile")==null?null:paramsMap.get("mobile").toString();
		String userId = paramsMap.get("userId")==null?null:paramsMap.get("userId").toString();
		
		//计算到期日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(sdf.format(new Date())));
		Date validityPeriodDate = null;
		cal.add(Calendar.DATE, channelWelfareParameters.getExpires()-1);
		validityPeriodDate = cal.getTime();
		
		//注册获取加息券
		RateRisesCoupons fixedCoupons = getBaseInfo(userId, mobile);
		//定期加息券参数拼接
		//到期日期
		fixedCoupons.setValidity_period(validityPeriodDate);
		//产品id
		fixedCoupons.setProduct_id(channelWelfareParameters.getProductCategory());
		//加息利率
		fixedCoupons.setRate_rises(channelWelfareParameters.getRiseRate());
		//加息天数
		fixedCoupons.setRate_rises_days(Integer.valueOf(channelWelfareParameters.getRiseDays()));
		
		//短期标加息天数 根据参数控制，0－表示不启用，1－表示启用短期标送加息券
		String fhjxq_control=findParamValueByName("fhjxq_control");
		if(fhjxq_control==null||"".equals(fhjxq_control.trim())){
			fhjxq_control="0";
		}
		if("1".equals(fhjxq_control) && "108".equals(channelWelfareParameters.getProductCategory())){
			fixedCoupons.setRemanDays(Integer.valueOf(channelWelfareParameters.getRiseDays()));
		}else{
			fixedCoupons.setRemanDays(0);
		}
		//获取途径
		fixedCoupons.setGet_way("1");
		//生成加息券
		rateRisesCouponsDao.insertRateRisesCoupons(fixedCoupons);
		return null;
	}
	
	/**
	 * 拼接基础参数
	 * @param userId
	 * @param mobile
	 * @param coupons
	 * @return
	 * @throws ParseException 
	 */
	private RateRisesCoupons getBaseInfo(String userId,String mobile) throws ParseException{
		RateRisesCoupons coupons = new RateRisesCoupons();
		coupons.setStatus("0");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		coupons.setCreate_time(sdf.parse(sdf.format(new Date())));
		coupons.setUser_id(StringUtils.isBlank(userId)?null:Long.parseLong(userId));
		coupons.setMobile(mobile);
		return coupons;
	}
	
	public String findParamValueByName(String key) throws Exception {
		List<String> listStr = new ArrayList<String>();
		listStr.add(key);
		//去BL查询系统参数表
		List<Map<String, Object>> extractFeeRate = rateRisesCouponsDao.queryParamName(listStr);
		if (extractFeeRate !=null && extractFeeRate.size() > 0) {
			return (String) extractFeeRate.get(0).get("VALUE");
		}
		return null;
	}

}
